﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.IO;
using SharpFoundation.PlistLib;
using SharpFoundation.XibLib.Decoders;
using SharpFoundation.XibLib;

namespace SharpFoundation.XibViewer
{
	public partial class XibRootForm : Form
	{
		public XibRootForm()
		{
			InitializeComponent();
		}

		public void LoadXib(string filename)
		{
			Text = Path.GetFileName(filename);

			XibUnarchiver unarchiver = new XibUnarchiver();
			PlistItem rootItem = unarchiver.DecodeRootObject(filename);
			
			itemListView.BeginUpdate();

			object[] rootObjects =
				(object[])rootItem.KeyedItems["IBDocument.RootObjects"].ObjectValue;
			foreach (object rootObject in rootObjects)
				if (rootObject != null)
					itemListView.Items.Add(new XibObjectListViewItem(rootObject, listImageList));

			itemListView.EndUpdate();
		}

		private void itemListView_ItemActivate(object sender, EventArgs e)
		{
			if (itemListView.SelectedItems.Count < 0)
				return;

			XibObjectListViewItem item = itemListView.SelectedItems[0]
				as XibObjectListViewItem;
			item.Activate();
		}
	}
}
